from flask import Blueprint,request,jsonify
import json
from db import db1
from utils.redisc import red
bp_work = Blueprint('workflow',__name__)

@bp_work.route('worklist')
def worklist():
    sql = "select * from workflow"
    res = db1.findall(sql)
    return  jsonify({'code':200,'list':res})

# 获取数据源
@bp_work.route('getsourcedata')
def getsourcedata():
    # 从redis取，如果有直接返回
    values = red.find_set('dataresouce')
    if values:
        list = eval(values)
    else:
        sql="select * from data_source"
        res = db1.findall(sql)
        list=[]
        if res:
            for i in res:
                dict = {'id':i['id'],'name':i['name']}
                sql1 = "select id,name,type from %s"%(i['tablename'])
                subres = db1.findall(sql1)
                dict['mes']=subres
                list.append(dict)
                print(list)
        red.insert_set('dataresouce',json.dumps(list))
    return jsonify({'code':200,'list':list})


@bp_work.route('/setparmas',methods=['POST'])
def setparmas():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    sql = "update workflow set parmas='%s' where id=%d "%(data['mes'],int(data['id']))
    db1.update(sql)
    return jsonify({'code':200})

@bp_work.route('/getparmas')
def getparmas():
    id  = request.args.get('id')
    sql = "select parmas from workflow where id=%d"%(int(id))
    res = db1.find(sql)
    # print(res)
    return jsonify({'code':200,'list':res})


@bp_work.route('/holiday',methods=['POST'])
def holiday():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    print(data)
    sql = "insert into "
    return jsonify({'code':200})